﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Suzsoft.Smart.Data;
using ESOL.Common;

namespace ESOL.WinUI
{
    public partial class frmLoginSetting : BaseForm.frmBaseDialogyForm
    {
        #region vars
        private ConnConfigurationManager connConfigurationManager;
        private List<ConnConfiguration> connConfigurations;
        private ConnConfiguration connConfigurationSelected;
        private CrudFlag crudFlag;
        #endregion

        #region ctors
        public frmLoginSetting()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void SetInputProperty(bool isDisable)
        {
            if (isDisable==false)
            {
                txtConfigurationName.Enabled = true;
                cmbDBType.Enabled = true;
                txtDBAddress.Enabled = true;
                txtDBName.Enabled = true;
                txtDBUser.Enabled = true;
                txtDBPwd.Enabled = true;
                txtDBServerURL.Enabled = true;
            }
            else
            {
                txtConfigurationName.Enabled = false;
                cmbDBType.Enabled = false;
                txtDBAddress.Enabled = false;
                txtDBName.Enabled = false;
                txtDBUser.Enabled = false;
                txtDBPwd.Enabled = false;
                txtDBServerURL.Enabled = false;
            }
        }
        private void BindListBox()
        {
            connConfigurations = connConfigurationManager.GetConfigurations();
            //绑定ListBox
            clbConfiguration.DataSource = connConfigurations;
            clbConfiguration.DisplayMember = "ConfigName";
            clbConfiguration.ValueMember = "ConfigName";
            for (int i = 0; i < connConfigurations.Count; i++)
            {
                if (connConfigurations[i].IsApply == true)
                {
                    clbConfiguration.SetItemChecked(i, true);
                }
                else
                {
                    clbConfiguration.SetItemChecked(i, false);
                }
            }
        }
        private void SetConfiguration()
        {
            if (connConfigurationSelected == null)
            {
                connConfigurationSelected = new ConnConfiguration();
            }
            connConfigurationSelected.ConfigName = txtConfigurationName.Text.Trim();
            connConfigurationSelected.DBType = cmbDBType.Text.ToString();
            connConfigurationSelected.DBAddress = txtDBAddress.Text.Trim();
            connConfigurationSelected.DBName = txtDBName.Text.Trim();
            connConfigurationSelected.DBUser = txtDBUser.Text.Trim();
            connConfigurationSelected.DBEncryptedPwd = txtDBPwd.Text.Trim();
            connConfigurationSelected.ServerURL = txtDBServerURL.Text.Trim();
        }
        #endregion

        #region events
        private void frmLoginSetting_Load(object sender, EventArgs e)
        {
            connConfigurationManager = new ConnConfigurationManager();
            BindListBox();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (connConfigurationSelected == null)
            {
                return;
            }
            SetInputProperty(false);
            crudFlag = CrudFlag.修改;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            SetInputProperty(false);
            txtConfigurationName.Text = string.Empty;
            txtDBAddress.Text = string.Empty;
            txtDBName.Text = string.Empty;
            txtDBPwd.Text = string.Empty;
            txtDBUser.Text = string.Empty;
            txtDBServerURL.Text = string.Empty;
            crudFlag = CrudFlag.新增;
            connConfigurationSelected = null;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //设置发往业务层的数据源
            SetConfiguration();
            if (connConfigurationManager.SaveConfiguration(connConfigurationSelected, crudFlag))
            {
                MessageBox.Show("Save Success!");
                SetInputProperty(true);
                crudFlag = CrudFlag.查询;
                BindListBox();
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (connConfigurationSelected == null)
            {
                return;
            }
            if (connConfigurationManager.ApplyConfiguration(connConfigurationSelected))
            {
                MessageBox.Show("Save success!");
                BindListBox();
            }
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtDBAddress.Text))
            {
                MessageBox.Show("数据库源不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (CommonFunctions.IPCheck(txtDBAddress.Text) == false)
            {
                MessageBox.Show("格式不正确！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrEmpty(txtDBName.Text))
            {
                MessageBox.Show("名称不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrEmpty(txtDBUser.Text))
            {
                MessageBox.Show("用户不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrEmpty(txtDBPwd.Text))
            {
                MessageBox.Show("密码不可为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                DataAccessConfiguration config = new DataAccessConfiguration();
                config.DBType = cmbDBType.Text;
                config.Parameters["server"] = txtDBAddress.Text;
                config.Parameters["datasource"] = txtDBName.Text;
                config.Parameters["user"] = txtDBUser.Text;
                if (connConfigurationSelected != null)
                {
                    config.Parameters["pwd"] = connConfigurationSelected.DBUnencryptedPwd;
                }
                else
                {
                    config.Parameters["pwd"] = txtDBPwd.Text;
                }
                DataAccessBroker dar = DataAccessFactory.Instance(config);
                dar.Close();
                MessageBox.Show("测试连接成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {
                MessageBox.Show("测试连接失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnWsTest_Click(object sender, EventArgs e)
        {

        }

        private void clbConfiguration_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (connConfigurations.Count == 0)
            {
                return;
            }
            if (clbConfiguration.SelectedIndex < 0)
            {
                return;
            }
            connConfigurationSelected = connConfigurationManager.GetConfigurationSelected(clbConfiguration.SelectedIndex);
            if (connConfigurationSelected == null)
            {
                return;
            }
            txtConfigurationName.Text = connConfigurationSelected.ConfigName;
            cmbDBType.SelectedItem = connConfigurationSelected.DBType;
            txtDBAddress.Text = connConfigurationSelected.DBAddress;
            txtDBName.Text = connConfigurationSelected.DBName;
            txtDBPwd.Text = connConfigurationSelected.DBEncryptedPwd;
            txtDBUser.Text = connConfigurationSelected.DBUser;
            txtDBServerURL.Text = connConfigurationSelected.ServerURL;
            SetInputProperty(true);
            crudFlag = CrudFlag.查询;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (connConfigurationSelected == null)
            {
                return;
            }
            if (connConfigurationManager.SaveConfiguration(connConfigurationSelected, CrudFlag.删除))
            {
                MessageBox.Show("Save success!");
                BindListBox();
            }
        }
        #endregion
    }
}
